package com.szbber.medical.board.ui.fragment.dialog

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.orhanobut.logger.Logger
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.PatientDetailsBean
import com.szbber.medical.board.R
import com.szbber.medical.board.databinding.BoardFragmentPatiendetailsBinding
import com.szbber.medical.board.databinding.BoardItemCarelabelBinding
import com.szbber.medical.board.viewModel.PatientDetailsFragmentViewModel
import kotlinx.android.synthetic.main.board_fragment_patiendetails.*

class PatientDetailsFragment :
    BaseVMFragment<BoardFragmentPatiendetailsBinding, PatientDetailsFragmentViewModel>(),
    View.OnClickListener {


    var cureNo: String? = null

    companion object {
        fun getInstance(cureNo: String?): PatientDetailsFragment {
            val sf = PatientDetailsFragment()
            sf.cureNo = cureNo
            return sf
        }
    }

    override fun initVM(): PatientDetailsFragmentViewModel =
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            PatientDetailsFragmentViewModel::class.java)

    override fun getLayoutResId(): Int = R.layout.board_fragment_patiendetails

    override fun initView(savedInstanceState: Bundle?) {

        mBinding.patientdetailsfragmentVM = mViewModel
        mViewModel.getPatientInfo(activity, cureNo)
        rlNurseLeve.setOnClickListener(this)
        rlDept.setOnClickListener(this)
        rlDoctorName.setOnClickListener(this)
        rlNurseName.setOnClickListener(this)
        rlInTimer.setOnClickListener(this)
        rlDiagnose.setOnClickListener(this)
        tvAdd.setOnClickListener(this)
        mViewModel.patientDetailsBean.observe(this) {
          when(it.data.patientVo.outlay_type){
             1-> tvMedicareType.text ="医保"
             2-> tvMedicareType.text ="自费"
             3-> tvMedicareType.text ="新农合"
          }
            it.data.nurseLevel?.bg_color?.apply {
                Logger.e("bg_color"+this)
                rlNurseLeve.setBackgroundColor(Color.parseColor(this))
            }
            it.data.nurseLevel?.font_color?.apply {
                Logger.e("font_color"+this)
                tvNurseLeve.setTextColor(Color.parseColor(this))
            }
            observeRecyclerView(it)
        }
    }

    private fun observeRecyclerView(data: PatientDetailsBean) {
        val adapter = object :
            BaseQuickAdapter<PatientDetailsBean.BedSideCareLabelVo, BaseViewHolder>(R.layout.board_item_carelabel,
                data.data.bedSideCareLabelVo.toMutableList()) {
            override fun convert(holder: BaseViewHolder,
                item: PatientDetailsBean.BedSideCareLabelVo) {
                val binding =
                    DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemCarelabelBinding
                binding.item = item
                binding.rlLabel.setBackgroundColor(Color.parseColor(item.bg_color))
                binding.tvName.setTextColor(Color.parseColor(item.font_color))
                binding.executePendingBindings()
            }
        }
        mRecyclerView.adapter = adapter
        val manager = LinearLayoutManager(activity)
        mRecyclerView.layoutManager = manager
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {

            R.id.rlNurseLeve -> mViewModel.getNurseLevel(activity, cureNo)
            R.id.rlDept -> mViewModel.getDeptList(activity, cureNo)
            R.id.rlDoctorName -> mViewModel.getDoctorList(activity, cureNo)
            R.id.rlNurseName -> mViewModel.getNurseList(activity, cureNo)
            R.id.rlInTimer -> mViewModel.etInTimer(activity, cureNo)
            R.id.tvAdd -> mViewModel.getCareLevelList(activity, cureNo)

        }
    }

}